<?php

class CatalogController extends Zend_Controller_Action
{
    /**
     * Item id
     * @var integer
     */
    private $id = null;

    /**
     * Controller helper
     * @var Zend_Controller_Action_HelperBroker
     */
    private $itemsLoader = null;

    /**
     * News model
     * @var Application_Model_News
     */
    private $model = null;
	
    /**
     * Order list
     * @var string
     */
    private $order = 'id DESC';
    
    /**
     * Limit of items
     * @var integer
     */
    private $limit = 2;

    /**
     * Current page
     * @var integer
     */
    private $pageNumber = 1;
    
    /**
     * List of items
     * @var array
     */
    private $items;
    
    /**
     * Current item
     * @var object 
     */
    private $item;
    
    /**
     * Count of items result
     * @var integer
     */
    private $count;
    
    /**
     * Count of pages
     * @var integer
     */
    private $pages;
	
    /**
     * Url for page
     * @var string
     */
    private $pageUrl = 'catalog';
    
    /**
     * Current category name
     * @var string
     */
    private $categoryName = null;
    
    /**
     * Current category
     * @var Application_Model_CatalogCategories
     */
    private $category = null;
        
    /**
     * (non-PHPdoc)
     * @see Controller/Zend_Controller_Action::init()
     */
    public function init()
    {
        $this->model = new Application_Model_Catalog();
    	$this->itemsLoader = $this->_helper->itemsLoader($this->model);
    	
		try
		{
	    	$this->page = $this->itemsLoader->loadPage($this->pageUrl);
    	}
		catch(Exception $e)
    	{
			throw new Zend_Controller_Exception($e->getMessage());
    	}
    	
        $this->id = (int)$this->_getParam('id');
		$this->categoryName = $this->_getParam('category');
		
		if (!empty($this->categoryName))
		{
			$this->getCategory();
		}
    	
    	$pageNumber = $this->_getParam('page');
    	
    	if (!empty($pageNumber))
    	{
    		$this->pageNumber = $pageNumber;
    	}
    }

    public function indexAction()
    {
        $this->getList();
    }
    
    /**
     * Action for catalog item page
     */
    public function itemAction()
    {
    	if (isset($this->id))
    	{
    		try
    		{
	    		$this->item = $this->itemsLoader->getItem($this->id);
    		}
    		catch(Exception $e)
    		{
    			throw new Zend_Controller_Exception($e->getMessage());
    		}
    	}
    	else
    	{
    		throw new Zend_Controller_Exception("Can't find item with id #" . $this->id);
    	}	
    }
    
    /**
     * Get products list
     * @param $fields
     */
    public function getList($fields = null)
    {
    	if (!empty($this->category))
		{
			$fields = array(
				'category' => $this->category->id 
			);
		}

    	try
    	{
			$data = $this->itemsLoader->getList($fields, $this->order, $this->limit, $this->pageNumber);
    	}
       	catch(Exception $e)
    	{
    		throw new Zend_Controller_Exception($e->getMessage());
    	}
    	   	
    	$this->items = $data->items;
    	$this->count = $data->count;
    	$this->pages = ceil($data->count/$this->limit);
    }
    
    /**
     * Get products list
     * @param $fields
     */
    public function getCategory()
    {
		$categoryModel = new Application_Model_CatalogCategories();
		$helper = clone $this->itemsLoader;
		
		try
		{
			$this->category = $categoryModel->getCategory($helper, $this->categoryName);			
		}
		catch(Exception $e)
    	{
			throw new Zend_Controller_Exception($e->getMessage());
    	}
    }
    
    public function postDispatch()
    {
		$this->view = $this->itemsLoader->setPageParameters($this->view, $this->page);
		$this->view->page = $this->page;
    	$this->view->item	= $this->item;
    	$this->view->items	= $this->items;
    	$this->view->count	= $this->count;
    	$this->view->pageNumber	= $this->pageNumber;
    	$this->view->pages	= $this->pages;
    	
    	$this->view->categoryName = $this->categoryName;
    	$this->view->category = $this->category;
    }
}